USE [QLDL]
GO

/****** Object:  StoredProcedure [dbo].[sp_ThemMatHang]    Script Date: 01/03/2011 15:31:55 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_ThemMatHang]
	@TenMatHang ntext, @MaDonViTinh nvarchar(10), @DonGia money
as
begin
	if exists(select * from MATHANG where TenMatHang like @TenMatHang)
		return 1
		
	declare @MaMatHang nvarchar(10)
	select @MaMatHang = MaMatHang from MATHANG order by (CAST(RIGHT(MaMatHang, LEN(MaMatHang) - 2) as int)) asc
	if (@MaMatHang is not null)
		set @MaMatHang = N'MH' + CAST((CAST(RIGHT(@MaMatHang, LEN(@MaMatHang) - 2) as int) + 1) as nvarchar)
	else
		set @MaMatHang = N'MH1'
	
	insert into MATHANG values(@MaMatHang, @TenMatHang, @MaDonViTinh, @DonGia)
	return 0
end
GO


